/**
 * 
 */
package testcases;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;

import org.junit.Assert;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;

import com.trentech.billcalc.Biller;
import com.trentech.billcalc.DateGroup;
import com.trentech.billcalc.Entity;
import com.trentech.billcalc.FinanceManager;
import com.trentech.billcalc.FinanceManagerAdapter;
import com.trentech.billcalc.Lease;
import com.trentech.billcalc.Resident;
import com.trentech.billcalc.event.ArriveEvent;
import com.trentech.billcalc.event.BillSpecificEvent;
import com.trentech.billcalc.event.Event;
import com.trentech.billcalc.event.EventFactory;
import com.trentech.billcalc.event.InvalidResultException;
import com.trentech.billcalc.gui.BillCalcFrame;
import com.trentech.billcalc.gui.Messages;
import com.trentech.billcalc.gui.ToolTipManager;
import com.trentech.billcalc.gui.BillCalcFrame.ResidentSelectionListener;



/**
 * @author marieputri
 *
 */
public class OtherTest 
{

    public static void TestOtherComponents(FinanceManager fm) {
        TestBiller();
        TestBillEvent();
        TestEventFactory();
        TestFinanceManagerAdapter(fm);
        TestFinanceManager_Remove(fm);
        TestToolTipManager(fm);
    }


    public static void TestBiller() {
        Biller biller = new Biller("Telephone Bill", MainTest.addDays(1));

        Assert.assertEquals("Telephone Bill", biller.toString());
        Assert.assertEquals("Telephone Bill", biller.getName());
        Assert.assertEquals(MainTest.addDays(1), biller.getFirstBillDay());
        Assert.assertEquals(MainTest.addDays(1), biller.getLastBillDay());
        Assert.assertEquals(BigDecimal.valueOf(0), biller.getLastBill());
        Assert.assertEquals(BigDecimal.valueOf(0), biller.getTotalBilled());
        Assert.assertTrue(biller.isAfterConsumption());
        Assert.assertFalse(biller.isFirstBillArrived());
        Assert.assertFalse(biller.isFixedCost());

        Biller biller2 = new Biller("TV Bill", MainTest.addDays(2));
        Assert.assertFalse(biller2.equals(biller));
        Assert.assertFalse(biller2.equals(null));
        Assert.assertFalse(biller2.equals(""));

        biller2 = new Biller(null, MainTest.addDays(2));
        assertTrue(!biller2.equals(biller));

        biller2 = new Biller("Telephone Bill", MainTest.addDays(1));
        assertTrue(biller2.equals(biller));
    }


    public static void TestBillEvent() {
        BillSpecificEvent bse = new BillSpecificEvent(MainTest.addDays(2),
                "Endok", 10000, "Donkey", "Telephone Bill");
        BillSpecificEvent bse2 = new BillSpecificEvent(MainTest.addDays(0),
                "Bird", 3000, "Duck", "TV Bill");
        assertNotSame(0, bse2.compareTo(bse));

        bse2.setDate(MainTest.addDays(2));
        assertNotSame(0, bse2.compareTo(bse));

        bse2.setName("Endok");
        assertNotSame(0, bse2.compareTo(bse));

        bse2.setAmount(10000);
        assertNotSame(0, bse2.compareTo(bse));

        bse2.setOwerName("Donkey");
        assertNotSame(0, bse2.compareTo(bse));

        bse2.setBillName("Telephone Bill");
        assertEquals(0, bse2.compareTo(bse));
    }


    public static void TestEventFactory() {
        EventFactory ev = EventFactory.StartLeaseEvent;

        List<String> ls = new ArrayList<String>();
        ls.add("amount");
        ls.add("canSublet");
        ls.add("date");
        ls.add("deposit");
        ls.add("generated");
        ls.add("leaseBreakPenalty");
        ls.add("leaseEnd");
        ls.add("name");
        ls.add("payerName");
        ls.add("paymentFrequency");
        ls.add("roomDescription");
        ls.add("saved");

        assertTrue(ev.isMoneyProperty("deposit"));
        assertEquals("Deposit", ev.getLocalizedName("deposit"));
        assertEquals(
                "The amount of money the tenant pays the landlord for the deposit.",
                ev.getLocalizedDescription("deposit"));
        assertEquals(ls, ev.getPropertyNames());
    }


    public static void TestFinanceManagerAdapter(FinanceManager fm) {
        FinanceManagerAdapter fma = new FinanceManagerAdapter();
        fma.dataReset();
        fma.eventsProcessed();

        fm.removeFinanceManagerListener(fma);
    }


    public static void TestFinanceManager_Remove(FinanceManager fm) {
        ArriveEvent av = new ArriveEvent(MainTest.addDays(1), "Fish");
        EventData.SaveEvent(fm, av);

        int eventNum = fm.getEvents().size();
        fm.removeEvent(av);
        assertEquals(eventNum-1, fm.getEvents().size());
    }
    
        
    public static void TestToolTipManager(FinanceManager fm) {
        ToolTipManager ttm = new ToolTipManager(); 
        ttm = new ToolTipManager(new BillCalcFrame(fm));
        ttm.setText(Messages.getUnpaidEstimate("Gass Bill"));
    }

    
	public static void TestOthers(FinanceManager fm) throws Exception
	{
		Event ev = EventData.ListArrive.get(0);
		ev.addPropertyChangeListener(new PropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				// TODO Auto-generated method stub
				
			}
		});
		
		ev.removePropertyChangeListener(new PropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				// TODO Auto-generated method stub
				
			}
		});
		
		InvalidResultException ire = new InvalidResultException(ev, "name", "Name is invalid");
		System.out.println(ire.getSource() + ire.getPropertyName() + ire.getErrorMessage());
		
		DateGroup leasePeriod = new DateGroup();
		leasePeriod.startRange(MainTest.addDays(0));
		leasePeriod.endRange(MainTest.addDays(10));
		
		Lease lease = new Lease(new Entity("Controller"), new Entity("Payer"), leasePeriod, 10000, 500, true, null, "Room Description", 1000, 2);
		lease.setLeaseBreakPenalty(2000);
		lease.setLeasePeriod(leasePeriod);
		
		lease.isCanSublet();
		lease.getRoomDescription();
		lease.getParentLease();
		
		BillCalcFrame bcf = new BillCalcFrame(fm);
		ResidentSelectionListener rsl = bcf.new ResidentSelectionListener();
        JList residentList = new JList(fm.getResidents().toArray());
        residentList.setSelectedIndex(0);
        rsl.valueChanged(new ListSelectionEvent(residentList, 0, 3, true));
        residentList.setSelectedIndices(new int[] {0,1});
        rsl.valueChanged(new ListSelectionEvent(residentList, 0, 3, true));
	}
}
